define(['app'],function(app){
    app.controller('BookMainCtrl' ,['$scope','$rootScope', '$timeout', '$http','$q','bookMainService',
       function ($scope, $rootScope, $timeout,$http, $q,bookMainService) {  
        
        //代码书写步骤
        //1.用【$scope】定义公共类model,和页面绑定的model
        //2.用【var】定义controller内部用的变量
        //3.用【$scope】和【var】定义方法，注意区分两者定义的方法的区别
        //4.最后定义【init】初始化方法，并调用，进行页面的初始化加载
        //定义规则，按可用性分忧优先级，公共的在前，private的在后
        $scope.indexCountData = {};
        $scope.legendData = [];//
        $scope.xAxisData = [];//月份
        $scope.seriesData = [];//显示数据

        //生成indexCountData 
        var createCountData = function(){
            for(var i=0;i<$scope.indexCountData.data.length;i++){
                $scope.legendData.push($scope.indexCountData.data[i].cate);
                var seriesModel = {
                    name:$scope.indexCountData.data[i].cate,
                    type:'line',
                    stack: '总量',
                    areaStyle: {normal: {}},
                    data:$scope.indexCountData.data[i].count
                };
                $scope.seriesData.push(seriesModel);
            }  
        }

        $scope.noteListData = [];
        $scope.retrNoteList = function(){
            bookMainService.retrNoteList().then(
                function(response){
                    if(response.status == 200){
                        $scope.noteListData = response.data;
                    }
                },
                function(code){
                    alert("系统出现问题了！");
                }
            );
        }

        // $scope.indexCountData = {};
        $scope.retrIndexCount = function(){
            bookMainService.retrIndexCount().then(
                function(response){
                    $scope.indexCountData = response;
                    createCountData();//生成model
                },
                function(code){
                    alert("系统出现问题了！");
                }
            );
        }       
       
        //count 控件
        $scope.option = {
            tooltip : {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: '#6a7985'
                    }
                }
            },
            legend: {
                // data:['文学','科技','心理','历史','传记']//bookType
                data:$scope.legendData
            },
            toolbox: {
                feature: {
                    saveAsImage: {}
                }
            },
            grid: {
                left: '10%',
                right: '10%',
                bottom: '10%',
                top:'10%',
                containLabel: false
            },
            xAxis : [//X轴 
                {
                    type : 'category',
                    boundaryGap : false,
                    data : ['一月','二月','三月','四月','五月','六月','七月','八月','九月','十月','十一月','十二月']
                }
            ],
            yAxis : [
                {
                    type : 'value'
                }
            ],
            series : $scope.seriesData
        }
        var init = function () {
            $scope.dashboardModel.isActiveMenu = 'main';//左侧菜单按钮赋初值
            $scope.retrNoteList();//查询阅读笔记
            $scope.retrIndexCount();//查询统计信息
        };

        init();
    }]);
});